package net.sf.rose.business.action.base;

import javax.servlet.http.HttpServletRequest;

import net.sf.rose.business.bean.base.Group;
import net.sf.rose.business.service.GroupSevice;
import net.sf.rose.jdbc.UserBean;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.OnlineUserListener;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author fengjian
 * @since 2014/03/06
 */
@Controller
@RequestMapping("base/user" )
public class UserController {

	private static String LIST_PAGE =  "base/user/list";
	private static String EDIT_PAGE = "base/user/edit";

	/**
	 * 
	 */
	@RequestMapping("/list.html")
	public ModelAndView list(HttpServletRequest request, Service service) {
		ModelAndView mav = new ModelAndView(LIST_PAGE);
		Group group = GroupSevice.getRoot();
		StringBuilder sb = new StringBuilder();
		this.addGroup(sb, group);
		mav.addObject("group", sb.toString());
		return mav;
	}

	private void addGroup(StringBuilder sb, Group group) {
		sb.append("<li><a onclick=\"treeClick(" + group.getId() + ");\" >").append(group.getText()).append("</a>");
		if (group.getChildGroup().size() > 0) {
			sb.append("<ul>");
			for (Group child : group.getChildGroup()) {
				this.addGroup(sb, child);
			}
			sb.append("</ul>");
		}
		sb.append("</li>");
	}

	/**
	 * 
	 */
	@RequestMapping("/add.html")
	public ModelAndView add(HttpServletRequest request, String parent) {
		Group group = GroupSevice.getService().getGroup(parent);
		ModelAndView mav = new ModelAndView(EDIT_PAGE);
		mav.addObject("parent", group);
		return mav;
	}

	/**
	 * 
	 */
	@RequestMapping("/edit.html")
	public ModelAndView edit(HttpServletRequest request, String node) {
		Group group = GroupSevice.getService().getGroup(node);
		Group parent = GroupSevice.getService().getParent(group);
		ModelAndView mav = new ModelAndView(EDIT_PAGE);
		mav.addObject("group", group);
		mav.addObject("parent", parent);
		return mav;
	}

	@RequestMapping("/save.html")
	public ModelAndView save(HttpServletRequest request, Group group, String parentID) {
		Group parent = GroupSevice.getService().getGroup(parentID);
		if (group.getId() == null || group.getId().equals("")) {
			parent.addChildGroup(group);
		} else {
			Group bak = GroupSevice.getService().getGroup(group.getId());
			bak.update(group);
		}
		ModelAndView mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "操作成功！");
		mav.addObject("callbackType", true);
		//mav.addObject("forwardUrl",);
		return mav;
	}

	@RequestMapping("/delete.html")
	public ModelAndView delete(HttpServletRequest request, String node) {
		Group group = GroupSevice.getService().getGroup(node);
		Group parent = GroupSevice.getService().getParent(group);
		parent.removeChildGroup(group.getId());
		ModelAndView mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "操作成功！");
		return mav;
	}

	@RequestMapping("/saveAll.html")
	public ModelAndView saveAll(HttpServletRequest request) {
		UserBean user = OnlineUserListener.getOnlineUser(request);
		GroupSevice.save(user);
		ModelAndView mav = new ModelAndView("ajaxDone");
		mav.addObject("statusCode", 200);
		mav.addObject("message", "操作成功！");
		return mav;
	}
}
